package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.OrderItem;
import com.example.exception.FloraException;
import com.example.mapper.OrderItemMapper;
import com.example.service.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements OrderItemService {
    @Autowired
    private OrderItemService orderItemService;

    @Override
    public IPage<OrderItem> getOrderItem(Long orderId, int pageNum, int pageSize) {
        LambdaQueryWrapper<OrderItem> qw = new LambdaQueryWrapper<>();
        Page<OrderItem> page = new Page<>(pageNum,pageSize);
        qw.eq(OrderItem::getOid,orderId);
        return page(page,qw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderItem addOrderItem(OrderItem orderItem) throws FloraException {
        try {
            this.baseMapper.insert(orderItem);
            return orderItem;
        } catch (Exception e) {
            throw new FloraException("添加订单商品失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOrderItem(Long orderId) throws FloraException {
        LambdaQueryWrapper<OrderItem> qw = new LambdaQueryWrapper<>();
        qw.eq(OrderItem::getOid,orderId);
        try{
            this.baseMapper.delete(qw);
        } catch (Exception e) {
            throw new FloraException("删除该订单下的商品失败"+e.getMessage());
        }
    }


}
